package com.lzc.syn;

/**
 * @author lizhichuan
 * @date 2024/7/10 21:25
 */
public class SynchronizedObjectCodeBlock2 implements Runnable{


     private static SynchronizedObjectCodeBlock2 instant = new SynchronizedObjectCodeBlock2();
     Object lock = new Object();

    @Override
    public void run() {
//        synchronized (lock){
        synchronized (this){
            System.out.println("我是对象锁的代码块形式。我叫：" + Thread.currentThread().getName());
            try {
                Thread.sleep(3000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println(Thread.currentThread().getName() + "运行结束");
        }

    }

    public static void main(String[] args) {
        Thread t1 = new Thread(instant);
        Thread t2 = new Thread(instant);
        t1.start();
        t2.start();
        while (t1.isAlive() || t2.isAlive()){

        }
        System.out.println("finished");
    }
}
